package com.huya.k12.translate.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huya.k12.translate.model.entity.Word;
import com.huya.k12.translate.model.query.WordQuery;
import com.huya.k12.translate.model.vo.DataTable;
import com.huya.k12.translate.model.vo.PageControl;
import com.huya.k12.translate.service.SVNService;
import com.huya.k12.translate.service.WordService;
import com.huya.k12.translate.util.SVNUtil;
import com.huya.k12.translate.util.XMLUtil;

@Controller
@RequestMapping("/admin/word")
public class WordController {
	
	@Autowired
	private WordService wordService;
	
	@Autowired
	private SVNUtil svnUtil;
	
	@Autowired
	private XMLUtil xmlUtil;
	
	@Autowired
	private SVNService svnService;
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true);
		binder.registerCustomEditor(Date.class, editor);
	}
	
	@RequestMapping(value="/word-list")
	public String list(Model model, String wordId){
		model.addAttribute("wordId", wordId);
		return "/admin/word/word-list";
	}
	
	@RequestMapping(value = "/edit-index")
	public String editIndex(Model model, String wordId){
		Word word = wordService.get(wordId);
		model.addAttribute("word",word);
		return "/admin/word/word-edit";
	}
	
	@RequestMapping(value = "/edit")
	public String edit(Model model, Word word){
		if(word != null){
			word.setStatus(1);
			wordService.edit(word);
		}
		return "redirect:/admin/word/word-list";
	}
	
	
	@RequestMapping(value="/getTableData")
	@ResponseBody
	public Map<String,Object> getTableData(HttpServletRequest request,DataTable dataTable,String orderBy,String wordId){
		
		PageControl pageControl = new PageControl(dataTable.getStart(), dataTable.getLength());
		WordQuery query = new WordQuery();
		query.setOrderBy(orderBy);
		query.setWordId(wordId);
		List<Word> wordList = wordService.listByQuery(query, pageControl);
		Integer dataCount = wordService.countData(query);
		Map<String,Object>map = new HashMap<String,Object>();
		map.put("data", wordList);
		map.put("recordsTotal", dataCount);
		map.put("recordsFiltered", dataCount);
		map.put("draw", dataTable.getDraw());
		return map;
	}
	
	@RequestMapping(value="/word-change-list")
	public String wordChangeList(Model model, String wordId){
		model.addAttribute("wordId", wordId);
		return "/admin/word/word-change-list";
	}
	
	@RequestMapping(value="/update-word-update")
	public String update(){
		updateWord();
		return "redirect:/admin/word/update-word-list";
	}
	
	@RequestMapping(value="/commit")
	@ResponseBody
	public void commit(){
		svnService.commit();
	}
	
	@RequestMapping(value="/word-change-edit-index")
	public String wordChangeEditIndex(Model model, String wordId){
		Word word = wordService.get(wordId);
		model.addAttribute("word", word);
		return "/admin/word/word-change-edit";
	}
	
	@RequestMapping(value="/word-change-edit")
	public String wordChangeEdit(Model model, Word word){
		if(word != null){
			word.setStatus(1);
			wordService.edit(word);
		}
		return "redirect:/admin/word/word-change-list";
	}
	
	@RequestMapping(value = "/getWordChange")
	@ResponseBody
	public Map<String,Object> getWordChange(DataTable dataTable, String orderBy, String wordId){
		PageControl pageControl = new PageControl(dataTable.getStart(), dataTable.getLength());
		WordQuery query = new WordQuery();
		query.setOrderBy(orderBy);
		query.setStatus(1);
		query.setWordId(wordId);
		List<Word> updateWordList = wordService.listByQuery(query, pageControl);
		Integer dataCount = wordService.countData(query);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", updateWordList);
		map.put("recordsTotal", dataCount);
		map.put("recordsFiltered", dataCount);
		map.put("draw", dataTable.getDraw());
		return map;
	}
	
	@RequestMapping(value="/initDB")
	public String initDB(){
		svnUtil.update();
		wordService.initDB();
		return "redirect:/admin/word/word-list";
	}
	
	private void updateWord(){
		svnUtil.update();
//		xmlUtil.updateDB();
		List<Word> addWord = xmlUtil.getAddWord();
		List<Word> updateWord = xmlUtil.getUpdateWord();
		List<Word> deleteWord = xmlUtil.getDeleteWord();
		if(addWord!=null && addWord.size()>0)
			wordService.addWord(addWord);
		if(updateWord!=null && updateWord.size()>0)
			wordService.updateWord(updateWord);
	}
	
	
}
